//
//  WHNewFeatureController.swift
//  microblog
//
//  Created by Apple on 15/11/29.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

class WHNewFeatureController: UICollectionViewController {

    //2 流水布局
    private var flowlayout = UICollectionViewFlowLayout()
    
    //3 需要显示多少个特性
    private let imageCount = 4
    
    //4.1 标识
    private let ReuseIdentifier = "ReuseIdentifier"
    
    //1 实现空参数的构造函数
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    init() {
        super.init(collectionViewLayout: flowlayout)
        
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        //4.0.1 
        prepareCollectionView()
        
    }
    
    //4.0.4
    private func prepareCollectionView() {
        //注册cell
        collectionView?.registerClass(WHNewFeatrueCell.self, forCellWithReuseIdentifier: ReuseIdentifier)
        //设置布局
        flowlayout.itemSize = view.bounds.size
        //设置滚动布局
        flowlayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        //设置间距
        flowlayout.minimumInteritemSpacing = 0
        flowlayout.minimumLineSpacing = 0
        //分页
        collectionView?.pagingEnabled = true
        //弹簧
        collectionView?.bounces = false
    }
    
    //4 数据方法
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageCount
    }
    //每个cell显示什么内容
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ReuseIdentifier, forIndexPath: indexPath) as! WHNewFeatrueCell
        
        cell.index = indexPath.item
        return cell
    }
    
    //7.1
    // 停下来,判断是最后一个cell的时候才做动画
    
    // 一个cell,colllectionView停下来,cell不可见的时候
    
    override func collectionView(CollectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        // 获取正在显示的cell的indexPath
        let visibleIndexPath = CollectionView.indexPathsForVisibleItems().last!
        if visibleIndexPath.item == imageCount - 1 {
            let cell = CollectionView.cellForItemAtIndexPath(visibleIndexPath) as! WHNewFeatrueCell
            cell.startBtnAnimation()
        }
        
    }
}

//4.2 自定义cell
class WHNewFeatrueCell: UICollectionViewCell {
    
    //4.3 第几个cell
    var index: Int = 0 {
        didSet {
          //显示图片
         bkgimageView.image = UIImage(named: "new_feature_\(index + 1)")
         startButton.hidden = true
        }
    }
    
    //5
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }
    
    //6.2点击按钮
    func buttonClick() {
        AppDelegate.switchViewController(true)
    }
    
    //6 准备UI
    private func prepareUI() {
        //添加子控件
        contentView.addSubview(bkgimageView)
        contentView.addSubview(startButton)
        // 添加约束
        bkgimageView.translatesAutoresizingMaskIntoConstraints = false
        startButton.translatesAutoresizingMaskIntoConstraints = false
        
        // 填充父控件
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[biv]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["biv" : bkgimageView]))
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[biv]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["biv" : bkgimageView]))
        // 开始按钮
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
        
    }
    
    
    // 7动画
    func startBtnAnimation() {
        //显示按钮
        startButton.hidden = false
        //设置按钮X/Y缩放都为0
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        //动画
        UIView.animateWithDuration(1, delay: 0.2, usingSpringWithDamping: 0.5, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                print("坑啊，这就是动画")
        }
    }
    
    //6.1 懒加载
    private lazy var bkgimageView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    //开始按钮
    private lazy var startButton: UIButton = {
        let button = UIButton()
        // 设置背景
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        // 设置文字
        button.setTitle("开始体验", forState: UIControlState.Normal)
        
        //点击事件
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
}